//
//  DKDetailImageCell.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/27.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

class DKDetailImageCell: DKAssetGroupDetailBaseCell {

    class override func cellReuseIdentifier() -> String {
        return "DKDetailImageCell"
    }
    
    let thumbnailImageView: UIImageView = {
        let thumbnailImageView = UIImageView()
        thumbnailImageView.isUserInteractionEnabled = true
        
        thumbnailImageView.contentMode = .scaleAspectFill
        thumbnailImageView.clipsToBounds = true
        
        return thumbnailImageView
    }()
    
    
    
    let selectButton:UIButton = {
        let selectButton = UIButton.init(type: UIButtonType.custom)
        selectButton.imageView?.contentMode = .scaleAspectFill
        selectButton.setImage(UIImage.init(named: "thumimage_select"), for: .selected)
        selectButton.setImage(UIImage.init(named: "thumimage_unselect"), for: .normal)
        return selectButton
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.thumbnailImageView.frame = self.bounds
        self.thumbnailImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//        self.thumbnailImageView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.showBigImage)))
        self.contentView.addSubview(self.thumbnailImageView)
        
        self.selectButton.frame = CGRect.init(x: self.bounds.maxX - 30 , y: 0, width: 30, height: 30)
        self.selectButton.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//        self.selectButton.addTarget(self, action: #selector(self.chooseImgClick(_:)), for: .touchUpInside)
        self.contentView.addSubview(self.selectButton)
        self.contentView.bringSubview(toFront: self.selectButton)
    }
    
    override var thumbnailImage: UIImage? {
        didSet {
            self.thumbnailImageView.image = self.thumbnailImage
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showBigImage(){
        self.asset!.fetchOriginalImageWithCompleteBlock { (image, info) in
            let photoItem = KSPhotoItem.init(sourceView: self.thumbnailImageView, image: image!)
            let browser = KSPhotoBrowser.init(photoItems: [photoItem], selectedIndex: UInt(self.index))
            browser.show(from: self.findController())
        }
    }
    
    override var isSelected: Bool{
        didSet{
            if super.isSelected{
                selectButton.isSelected = true
                self.thumbnailImageView.alpha = 0.5
                self.thumbnailImageView.layer.borderColor = UIColor.RGB(r: 255, g: 206, b: 80).cgColor
                self.thumbnailImageView.layer.borderWidth = 1
                
            }else{
                selectButton.isSelected = false
                self.thumbnailImageView.alpha = 1
                self.thumbnailImageView.layer.borderColor = UIColor.clear.cgColor
                self.thumbnailImageView.layer.borderWidth = 0
            }
        }
    }
    
//    @objc fileprivate func chooseImgClick(_ button:UIButton){
//        if button.isSelected{
//            button.isSelected = false
//            self.thumbnailImageView.alpha = 1
//            self.thumbnailImageView.layer.borderColor = UIColor.clear.cgColor
//            self.thumbnailImageView.layer.borderWidth = 0
//        }else{
//            button.isSelected = true
//            self.thumbnailImageView.alpha = 0.5
//            self.thumbnailImageView.layer.borderColor = UIColor.RGB(r: 255, g: 206, b: 80).cgColor
//            self.thumbnailImageView.layer.borderWidth = 1
//        }
//    }
    
}
